package cn.new7as.cas5s.tool.phut;

import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager


class BatGetUxctils {


    fun initReceiver(activity: Activity) {
        val asdfcw_batteryReceiver = BatteryReceiver()
        val asdfcw_intent = IntentFilter()
        asdfcw_intent.addAction(Intent.ACTION_BATTERY_CHANGED)
        asdfcw_intent.addAction(Intent.ACTION_BATTERY_LOW)
        asdfcw_intent.addAction(Intent.ACTION_BATTERY_OKAY)
        activity.registerReceiver(asdfcw_batteryReceiver, asdfcw_intent)
    }

    inner class BatteryReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent != null) {
                val health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1)
                val power = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
                val plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
                val asdfcw_status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
                val voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1)
                val asdfcw_batteryTechnology =
                    intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY)
                val temp =
                    intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1)
                val asdfcw_batteryHealth = getBatteryHealth(health)
                val asdfcw_charger = getPluged(plugged)
                val asdfcw_batteryStatus = getBatteryStatus(asdfcw_status)
                var asdfcw_isCharging = ""
                if (asdfcw_status == BatteryManager.BATTERY_STATUS_FULL || asdfcw_status == BatteryManager.BATTERY_STATUS_CHARGING) {
                    asdfcw_isCharging = "1"
                } else {
                    asdfcw_isCharging = "0"
                }

                val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
                val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
                val pct: Float =     level * 100 / scale.toFloat()
                listener?.setBatteryResult(
                    asdfcw_batteryHealth,
                    power.toString(),
                    asdfcw_charger,
                    asdfcw_batteryStatus,
                    voltage.toString(),
                    asdfcw_batteryTechnology,
                    temp.toString(),
                    asdfcw_isCharging,pct
                )
            }
            context?.unregisterReceiver(this)
        }
    }

    private fun getBatteryHealth(health: Int): String {
        return when (health) {
            BatteryManager.BATTERY_HEALTH_GOOD -> {
                "BATTERY_HEALTH_VERY_GOOD"
            }

            BatteryManager.BATTERY_HEALTH_COLD -> {
                "BATTERY_HEALTH_COLD"
            }

            BatteryManager.BATTERY_HEALTH_DEAD -> {
                "BATTERY_HEALTH_DEAD"
            }

            BatteryManager.BATTERY_HEALTH_OVERHEAT -> {
                "BATTERY_HEALTH_OVERHEAT"
            }

            BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE -> {
                "BATTERY_HEALTH_OVER_VOLTAGE"
            }

            BatteryManager.BATTERY_HEALTH_UNKNOWN -> {
                "BATTERY_HEALTH_UNKNOWN"
            }

            BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE -> {
                "BATTERY_HEALTH_UNSPECIFIED_FAILURE"
            }

            else -> {
                ""
            }
        }
    }

    private fun getPluged(plugged: Int): String {
        when (plugged) {
            BatteryManager.BATTERY_PLUGGED_AC ->
                return "AC charger."

            BatteryManager.BATTERY_PLUGGED_USB ->
                return "USB port"

            else -> {
                return ""
            }
        }
    }

    fun getBatteryStatus(status: Int): String {
        return when (status) {
            BatteryManager.BATTERY_STATUS_CHARGING ->
                "Charging"

            BatteryManager.BATTERY_STATUS_DISCHARGING ->
                "BATTERY_STATUS_DISCHARGING"

            BatteryManager.BATTERY_STATUS_FULL ->
                "Battery full"

            BatteryManager.BATTERY_STATUS_NOT_CHARGING ->
                "The battery is not charged"

            BatteryManager.BATTERY_STATUS_UNKNOWN ->
                "unknown state"

            else -> ""
        }
    }


    private var listener: OnBattCallBack? = null

    interface OnBattCallBack {
        fun setBatteryResult(
            bhjlk: String,
            maxPower: String,
            charger: String,
            batteryStatus: String,
            toString: String,
            batteryTechnology: String?,
            toString1: String,
            isCharging: String,
            pct: Float
        )
    }


    fun setOnBatteryListener(listener: OnBattCallBack) {
        this.listener = listener
    }
}